package net.worldscale.xp.wslib.urm;

import java.io.IOException;
import java.util.Hashtable;

import net.worldscale.xp.wslib.urm.io.Connector;
import net.worldscale.xp.wslib.urm.io.IConnection;
import net.worldscale.xp.wslib.urm.io.IConnectionFactory;

final class ImplConnector extends Connector {

	private final Hashtable<String, IConnectionFactory> mTable = new Hashtable<String, IConnectionFactory>();

	@Override
	public IConnectionFactory getConnFactory(String scheme) {
		return this.mTable.get(scheme);
	}

	@Override
	public IConnection open(String uri) throws IOException, BadUriException {
		final URI uriobj = URM.getInstance().newURI(uri);
		final String scheme = uriobj.getScheme();
		final IConnectionFactory f = this.getConnFactory(scheme);
		return f.open(uri);
	}

	@Override
	public void regConnFactory(IConnectionFactory f) {
		if (f != null) {
			String scheme = f.getScheme();
			this.mTable.put(scheme, f);
		}
	}

	@Override
	public void unregConnFactory(IConnectionFactory f) {
		if (f != null) {
			String scheme = f.getScheme();
			this.mTable.remove(scheme);
		}
	}

}
